package com.liao.controller;

import com.liao.bean.Order;
import com.liao.bean.Param;
import com.liao.bean.result.Code;
import com.liao.bean.result.Msg;
import com.liao.bean.result.PageResult;
import com.liao.bean.result.Result;
import com.liao.service.OrderService;
import com.liao.service.ParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/param")
public class ParamController {

    @Autowired
    private ParamService paramService;

    @ResponseBody
    @RequestMapping("/queryParamListByPage")
    public PageResult<Param> queryParamListByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                                  @RequestParam(defaultValue = "5") Integer pageSize,
                                                  @RequestParam(defaultValue = "") String paramKey) throws Exception {
        PageResult<Param> pageResult = paramService.queryParamListByPage(currentPage,pageSize,paramKey);
        return pageResult;
    }

    @ResponseBody
    @RequestMapping("/addParam")
    public Result addParam(@RequestBody Param param) throws Exception {

        paramService.addParam(param);
        return new Result(true, Code.PARAM_ADD_SUCCESS, Msg.PARAM_ADD_SUCCESS);

    }

    @ResponseBody
    @RequestMapping("/editParam")
    public Result editParam( @RequestBody Param param) throws Exception {
        paramService.editParam(param);
        return new Result(true,Code.PARAM_EDIT_SUCCESS, Msg.PARAM_EDIT_SUCCESS);
    }

    @ResponseBody
    @RequestMapping("/deleteParam")
    public Result deleteParam(String paramId) throws Exception {
        paramService.deleteParam(paramId);
        return new Result(true,Code.PARAM_DELETE_SUCCESS, Msg.PARAM_DELETE_SUCCESS);
    }

    @ResponseBody
    @RequestMapping("/deleteParamIds")
    public Result deleteParamIds(@RequestParam("paramIds[]") List<String> paramIds) throws Exception {
        for (String paramId : paramIds) {
            try {
                paramService.deleteParam(paramId);
            }catch (Exception e){
                e.printStackTrace();
                return new Result(false,Code.PARAM_DELETE_FAIL, Msg.PARAM_DELETE_FAIL);
            }

        }
        return new Result(true,Code.PARAM_DELETE_SUCCESS, Msg.PARAM_DELETE_SUCCESS);
    }
}
